# Use Debian-based Node.js 20 image (required for Dynatrace)
FROM node:20-bullseye-slim

# Dynatrace OneAgent arguments
#ARG DT_API_URL="https://gmo10609.live.dynatrace.com/api"
#ARG DT_API_TOKEN="dt0c01.BF3I2DU7AZRTGL3PBRZBQARD.PVCL5IXYKUV4LQRAN2C53GGXURG7LCUPHAY4ETP5R5RK3KYYHQYY4L63SSR7R3UL"
#ARG DT_ONEAGENT_OPTIONS="flavor=default&include=all"
#ENV DT_HOME="/opt/dynatrace/oneagent"

# Install dependencies first
RUN apt-get update && \
    apt-get install -y wget unzip ca-certificates && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

# Install Dynatrace OneAgent (MANDATORY - build fails if this fails)
#RUN mkdir -p "$DT_HOME" && \
#    wget -O "$DT_HOME/oneagent.zip" "$DT_API_URL/v1/deployment/installer/agent/unix/paas/latest?Api-Token=$DT_API_TOKEN&$DT_ONEAGENT_OPTIONS" && \
#    unzip -d "$DT_HOME" "$DT_HOME/oneagent.zip" && \
#    rm "$DT_HOME/oneagent.zip"

# Verify OneAgent installed correctly
#RUN test -f "/opt/dynatrace/oneagent/dynatrace-agent64.sh" || (echo "ERROR: Dynatrace OneAgent not installed correctly" && exit 1)

# Install PM2 globally
RUN npm install -g pm2

# Set working directory
WORKDIR /app

# Copy package files
COPY package.json ./

# Install dependencies
RUN npm install --no-cache && npm install nuxt@4.0.3

# Copy source code
COPY . .

RUN more .env
# Generate Prisma client and build
RUN npm run P_Generate && npm run build

# Expose port
EXPOSE 3000

# Start with Dynatrace OneAgent (MANDATORY)
#ENTRYPOINT ["/opt/dynatrace/oneagent/dynatrace-agent64.sh"]
CMD ["pm2-runtime", "start", ".output/server/index.mjs"]
